package com.yitao.cms.service.user.appUser;

import com.google.common.collect.Lists;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.skipper.base.bean.DataOutput;
import com.skipper.base.bean.PagedInput;
import com.skipper.base.bean.PagedOutput;
import com.skipper.base.data.querydsl.PredicateBuilder;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.constant.AppUserEnum;
import com.yitao.client.dto.user.UserInfoDto;
import com.yitao.client.model.user.QUserInfoModel;
import com.yitao.client.model.user.UserInfoModel;
import com.yitao.cms.service.order.CmsOrderService;
import com.yitao.cms.utils.BaseUtils;
import com.yitao.cms.utils.DateUtils;
import com.yitao.cms.vo.user.appUser.AppUserInfoDetailsVo;
import com.yitao.cms.vo.user.appUser.AppUserInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.List;

@Service
public class AppUserService extends GenericEntityService<UserInfoModel, UserInfoDto, QUserInfoModel> {
    public AppUserService(EntityManager entityManager) {
        super(entityManager);
    }

    private BaseUtils base = BaseUtils.getInstance();


    @Autowired
    private CmsOrderService orderService;

    @Autowired
    private AppUserAddressService appUserAddressService;

    /**
     * 分页查询用户列表
     *
     * @param userName
     * @param userPhone
     * @param pagedInput
     * @return
     */
    public PagedOutput<AppUserInfoVo> getAppUserList(String userName, String userPhone, PagedInput pagedInput) {
        List<AppUserInfoVo> userInfoDtoList = Lists.newArrayList();
        AppUserInfoVo appUserInfoVo = null;
        PagedOutput<UserInfoModel> userInfoModelPagedOutput = repository.pagingList(PredicateBuilder.conjunction().
                ifHasTextThenAnd(userName, () -> Q.nickName.contains(userName)).
                ifHasTextThenAnd(userPhone, () -> Q.phone.contains(userPhone)).
                get(), pagedInput, new OrderSpecifier<>(Order.DESC, Q.registerTime));
        if (userInfoModelPagedOutput != null && userInfoModelPagedOutput.getData() != null && userInfoModelPagedOutput.getData().size() != 0) {
            List<UserInfoModel> userInfoModelList = userInfoModelPagedOutput.getData();
            for (UserInfoModel userInfoModel : userInfoModelList) {
                appUserInfoVo = new AppUserInfoVo();
                BeanUtils.copyProperties(userInfoModel, appUserInfoVo);
                if (userInfoModel.getRegisterTime() != null) {
                    appUserInfoVo.setRegisterTime(DateUtils.getDateSecondFormat(userInfoModel.getRegisterTime()));
                }
                //查询消费金额
                if (userInfoModel.getId() != null) {
                    appUserInfoVo.setMonetary(orderService.getUserIdOrderMoney(userInfoModel.getId()));
                    appUserInfoVo.setOrderNumber(orderService.getOrderNumberByUser(userInfoModel.getId()));
                }

                userInfoDtoList.add(appUserInfoVo);
            }
        }
        return new PagedOutput<AppUserInfoVo>(pagedInput, userInfoModelPagedOutput.getTotalElements(), userInfoDtoList);
    }

    /**
     * app用户详情
     *
     * @param userId
     * @return
     */
    public DataOutput<AppUserInfoDetailsVo> getAppUserInfo(Long userId) {
        AppUserInfoDetailsVo appUserInfoDetailsVo = new AppUserInfoDetailsVo();
        if (userId == null) {
            return base.getErrorMsg();
        }
        UserInfoModel userInfoModel = null;
        List<UserInfoModel> userInfoModelList = repository.findAll(Q.id.eq(userId));
        if (userInfoModelList != null && userInfoModelList.size() != 0) {
            userInfoModel = userInfoModelList.get(0);
        }
        if (userInfoModel != null) {
            BeanUtils.copyProperties(userInfoModel, appUserInfoDetailsVo);
            //注册时间
            if (userInfoModel.getRegisterTime() != null) {
                appUserInfoDetailsVo.setRegisterTime(DateUtils.getDateSecondFormat(userInfoModel.getRegisterTime()));
            }
            //性别
            if (userInfoModel.getGender() != null) {
                AppUserEnum.GenderEnum anEnum = AppUserEnum.GenderEnum.getEnum(userInfoModel.getGender());
                if (anEnum != null) {
                    appUserInfoDetailsVo.setGender(anEnum.getName());
                }
            }
            appUserInfoDetailsVo.setAppUserAddressManagementVoList(appUserAddressService.findAddressByUserId(userId));
            appUserInfoDetailsVo.setUserId(userInfoModel.getId());
        }
        return base.getSuccessMsg(appUserInfoDetailsVo);
    }


    /**
     * 禁用或者启用app用户
     *
     * @param userId 用户ID
     * @return
     */
    @Transactional
    public DataOutput prohibitOrEnableAppUserInfo(Long userId) {
        if (userId == null) {
            return base.getErrorMsg();
        }
        UserInfoModel userInfoModel = null;
        List<UserInfoModel> userInfoModelList = repository.findAll(Q.id.eq(userId));
        if (userInfoModelList != null && userInfoModelList.size() != 0) {
            userInfoModel = userInfoModelList.get(0);
        }
        if (userInfoModel == null) {
            return base.getErrorMsg();
        }
        userInfoModel.setUserStatus(!userInfoModel.getUserStatus());
        repository.save(userInfoModel);
        return base.getSuccessMsg();
    }


    public UserInfoModel getAppUserInfoByUserId(Long buyerUserId) {
        return queryFactory.select(Q).from(Q).where(Q.id.eq(buyerUserId)).fetchOne();
    }
}
